import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
import { isLogined } from "../utils/tools";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
  },
  {
    path: "/list",
    name: "List",
    component: () => import("../views/List.vue"),
  },
  {
    path: "/productlist", //商品分类列表
    name: "ProductList",
    component: () => import("../views/ProductList.vue"),
  },
  {
    path: "/cart",
    name: "Cart",
    component: () => import("../views/Cart.vue"),
    meta: {
      needLogin: true,
    },
  },
  {
    path: "/user",
    name: "User",
    component: () => import("../views/User.vue"),
    meta: {
      needLogin: true, //用来验证本地是否登录，登录则跳转
      hideTabBar: false,
    },
  },
  {
    path: "/detail",
    name: "Detail",
    component: () => import("../views/Detail.vue"),
    meta: {
      hideTabBar: true,
    },
  },
  {
    path: "/reg",
    name: "Reg",
    component: () => import("../views/Reg.vue"),
    meta: {
      hideTabBar: true,
    },
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("../views/Login.vue"),
    meta: {
      hideTabBar: true,
    },
  },
  // {
  //   path: "/sub_order",
  //   name: "SubOrder",
  //   component: () => import("../views/SubOrder.vue"),
  //   meta: {
  //     hideTabBar: true, //去掉底部导航
  //   },
  // },
  {
    path: "/userpwd",
    name: "UserPwd",
    component: () => import("../views/UserPwd.vue"),
    meta: {
      hideTabBar: true, //去掉底部导航
    },
  },
  {
    path: "/userinfo",
    name: "UserInfo",
    component: () => import("../views/UserInfo.vue"),
    meta: {
      hideTabBar: true, //去掉底部导航
    },
  },
  {
    path: "*",
    name: "PageNotFount",
    component: () => import("../views/PageNotFount.vue"),
  },
];

const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => {
  if (to.meta.needLogin) {
    if (isLogined()) {
      next();
    } else {
      next({
        name: "Login",
      });
    }
  } else {
    next();
  }
});

export default router;
